// Copyright (c) 2010 Manuel Peinado Gallego <manuel.peinado@gmail.com>
// Distributed under the MIT license

#include "stdafx.h"
#include "WorldBuilder.h"
#include "TransformNode.h"


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TransformNode::setLocalTransform(const gx::Mat4 & m, 
                                      NodeEvent::Property prop) 
{
    ASSERT(prop >= NodeEvent::NodeTransform && prop <= NodeEvent::NodeScale);
    Transformable::setLocalTransform(m);
    invalidateGlobalTransform();
    notifyPropertyChange(prop);
}

void TransformNode::setLocalTransform(const gx::Mat4 & m) 
{
    setLocalTransform(m, NodeEvent::NodeTransform);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Serialization

void TransformNode::write(std::wostream& os) const
{
    Node::write(os);

    std::ostringstream oss;
    oss << m_localTransform;
    os << oss.str() << "\n";
}

void TransformNode::read(std::wistream& is)
{
    Node::read(is);

    // Read using a wide string
    std::wstring wstr;
    is >> wstr;

    // Convert to ansi string
    std::string str(CT2A(wstr.c_str()));

    // And finally feed it into Mat4, which only 
    // understands ansi strings
    std::istringstream iss(str);
    iss >> m_localTransform;

    invalidateGlobalTransform();
}

